﻿/*
 * Author:xmfdsh 11届网络部--邓世豪
 * DataTime：2014-6-9
 * Copyright © 2014 大学生勤工助学服务队 Powered by xmfdsh & 四小狼工作室.
 * 版权所有，违者必究
 */
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.OA
{
    public class UserController : Controller
    {
        #region 学生管理首页 +  ActionResult Index()
        /// <summary>
        /// 学生管理首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;

            List<MODEL.qgoa_user> list = OperateContext.Current.BLLSession.Iqgoa_userBLL.GetListBy(x => x.Id != null);
            ViewBag.List = list;
            return View();
        } 
        #endregion

        #region 新增学生页面 + ActionResult Create()
        /// <summary>
        /// 新增学生页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_user> list = OperateContext.Current.BLLSession.Iqgoa_userBLL.GetListBy(x => x.Id != null);
            ViewBag.list = list;
            List<MODEL.qgoa_department> departmentList = OperateContext.Current.BLLSession.Iqgoa_departmentBLL.GetListBy(x => x.Id != null);
            ViewBag.departmentList = departmentList;
            return View();
        } 
        #endregion

        #region 编辑学生（用户）页面 +  ActionResult Edit()
        /// <summary>
        /// 编辑学生（用户）页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_department> department_list = OperateContext.Current.BLLSession.Iqgoa_departmentBLL.GetListBy(x => x.Id != null);
            ViewBag.department_list = department_list;
            int Id = Convert.ToInt32(Request["id"]);
            if (Id != null)
            {
                MODEL.qgoa_user user = OperateContext.Current.BLLSession.Iqgoa_userBLL.GetListBy(x => x.Id == Id).FirstOrDefault();
                ViewBag.user = user;
            }
            return View();
        } 
        #endregion

        #region Action处理方法集合 + ActionResult Action()
        /// <summary>
        /// Action处理方法集合
        /// </summary>
        /// <returns></returns>
        public ActionResult Action()
        {
            string dowhat = Request.Form["dowhat"];
            if (dowhat.Equals("create_user"))
            {
                string Number = Request.Form["Number"];
                string Name = Request.Form["Name"];
                string Password = Request.Form["Password"];
                string DepartmentId = Request.Form["DepartmentId"];
                string RankId = Request.Form["RankId"];
                MODEL.qgoa_user user = new MODEL.qgoa_user() { Name = Name, Number = Number, Password = Password, DepartmentId = Convert.ToInt32(DepartmentId), RankId = Convert.ToInt32(RankId) };
                if (OperateContext.Current.BLLSession.Iqgoa_userBLL.Add(user) > 0)
                {
                    MODEL.qgoa_user u = OperateContext.Current.BLLSession.Iqgoa_userBLL.GetListBy(x => x.Name == Name).FirstOrDefault();
                    MODEL.qgoa_work_condition work = new MODEL.qgoa_work_condition() { Id = u.Id, Name = Name };
                    OperateContext.Current.BLLSession.Iqgoa_work_conditionBLL.Add(work);
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                else
                {
                    return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
                }

                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            } 

            if (dowhat.Equals("reset_password"))
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);
                MODEL.qgoa_user user=OperateContext.Current.BLLSession.Iqgoa_userBLL.GetListBy(x=>x.Id==Id).FirstOrDefault();
                user.Password = "123456";
                if ( OperateContext.Current.BLLSession.Iqgoa_userBLL.ModifyBy(user,x=>x.Id == Id,"Password") > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作成功", null, "");
            }

            if (dowhat == "delete_user")
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);

                if ( OperateContext.Current.BLLSession.Iqgoa_userBLL.DelBy(x=>x.Id==Id) > 0)
                {
                    OperateContext.Current.BLLSession.Iqgoa_work_conditionBLL.DelBy(x=>x.Id==Id);
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }
            if (dowhat.Equals("update_user"))
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);
                string Number = Request.Form["Number"];
                string Name = Request.Form["Name"];
                string RankId = Request.Form["RankId"];

                MODEL.qgoa_user user = OperateContext.Current.BLLSession.Iqgoa_userBLL.GetListBy(x=>x.Id==Id).FirstOrDefault();
                user.Number = Number;
                user.Name = Name;
                user.RankId = Convert.ToInt32(RankId);

                if (OperateContext.Current.BLLSession.Iqgoa_userBLL.ModifyBy(user, x => x.Id ==Id, "Number", "Number", "Name", "RankId") > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }
            return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
        }
        #endregion
    }
}
